<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;

use think\Session;
use think\Request;
use think\Cache;
use think\Config;
use think\Db;
use think\Controller;
use \CFormat;
use think\View;

/**
 * Description of User
 *
 * @author Administrator
 */
class User extends Controller {
    
    // 视图类实例
    protected $view;
    // Request实例
    protected $request;

    public function __construct()
    {
        parent::__construct();
        if (null === $this->view) {
            $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
        }
        if (null === $this->request) {
            $this->request = Request::instance();
        }
        // 用户ID
        defined('UID') or define('UID', Session::get(Config::get('rbac.user_auth_key')));
    }
    
    //用户列表
    public function show() {
        $data = array();
        $users= Db::name('staff')->select();
        foreach ($users as $_k=>$_v){
            if($_v['gender']===1){
                $users[$_k]['gender'] = '男';
            }else if($_v['gender']===2){
                $users[$_k]['gender'] = '女';
            }else{
                $users[$_k]['gender'] = '未知';
            }
            if($_v['status']===1){
                $users[$_k]['status'] = '正常';
            }else{
                $users[$_k]['status'] = '禁用';
            }
            $oneuser=array(
                'sid'       => $users[$_k]['id'],
                'name'      => $users[$_k]['name'],
                'gender'    => $users[$_k]['gender'],
                'mobile'    => $users[$_k]['mobile'],
                'status'    => $users[$_k]['status'],
                'role'      => $users[$_k]['role'],
                'gentime'   => CFormat::FormatDate($users[$_k]['gen_time'], 'Y-m-d H:i:s')
            );
            if(!$users[$_k]['gen_time']){
                $oneuser['gentime'] = '未登陆过';
            }
            $data[]=$oneuser;
        }
        $this->view->assign('users', $data);
        return $this->view->fetch();
    }
    
    //禁用&启用
    public function disableOrEnable(){
        $sid = input('get.sid', 0);
        if(!$sid){
            ajax_return_adv_error('未获取到用户ID');
        }
        $user=Db::name('staff')->find($sid);
        if($user['status']===0){
            Db::name('staff')->where('id',$sid)->update(['status'=>1]);
        }else{
            Db::name('staff')->where('id',$sid)->update(['status'=>0]);
        }
        return ajax_return_adv('操作成功');
    }
    
    //获取单个用户信息
    public function showOne(){
        $sid = input('get.sid', 0);
        $user=Db::name('staff')->field('id AS sid,name,gender,mobile,password,role')->find($sid);
        $this->view->assign('user', $user);
        $this->view->assign('role', Session::get('role'));
        return $this->view->fetch();
    }
    
    //新增或修改
    public function createOrUpdate(){
        $user = input('post.', array());
        if($user){
            if($user['id']){
                $u = Db::name('staff')->where('mobile',$user['mobile'])->where('id', '<>', $user['id'])->find();
                if($u){
                    return ajax_return_adv_error('手机号码重复！');
                }
                Db::name('staff')->where('id', $user['id'])->update($user);
            }
            else{
                $u = Db::name('staff')->where('mobile',$user['mobile'])->find();
                if($u){
                    return ajax_return_adv_error('手机号码重复！');
                }
                $user['status'] = 1;
                Db::name('staff')->insert($user);
            }
            return ajax_return_adv('操作成功');
        }
        else{
            return ajax_return_adv_error('操作失败！');
        }
    }
}
